package com.natalia.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;
import com.natalia.excel.constants.BusinessConstants;
import com.natalia.excel.entity.DownloadModel;
import com.natalia.excel.service.DownloadService;
import com.natalia.excel.utils.I18nCellWriteHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * @Author yang
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/download")
@RequiredArgsConstructor
public class DownloadController {

    private final DownloadService downloadService;

    /**
     * 下载excel模版
     */
    @PostMapping({"/template"})
    public void template(HttpServletResponse response){
        try {
            String filename = String.valueOf(System.currentTimeMillis())
                    .concat(BusinessConstants.EXPORT_EXCEL_NAME);
            response.setContentType(BusinessConstants.EXPORT_CONTENT_TYPE);
            response.setHeader(BusinessConstants.EXPORT_HEADER_ONE,
                    BusinessConstants.EXPORT_HEADER_TOW.concat(filename));
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            WriteSheet mainSheet = EasyExcel.writerSheet(0, BusinessConstants.BALANCE_TEMPLATE_SHEET)
                    .head(DownloadModel.class)
                    .registerWriteHandler(new I18nCellWriteHandler()).build();
            excelWriter.write(Lists.newArrayList(),mainSheet);
            excelWriter.finish();
        } catch (Exception e) {
            log.error("download template method exception:{}", ExceptionUtils.getStackTrace(e));
        }
    }

    /**
     * 下载word文档
     * @param response
     */
    @GetMapping("/word")
    public void download(HttpServletResponse response) {
        try {
            XWPFDocument document = downloadService.getWordXWPFDocument();
            String filename = String.valueOf(System.currentTimeMillis())
                    .concat(BusinessConstants.EXPORT_WORD_NAME);
            response.reset();
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(BusinessConstants.EXPORT_HEADER_ONE,
                    BusinessConstants.EXPORT_HEADER_TOW.concat(filename));
            OutputStream os = response.getOutputStream();
            document.write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
